package com.kalvan.core.exception;

import com.kalvan.core.response.Result;
import com.kalvan.core.system.SystemUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author chenliang
 */
@Getter
@Setter
@Slf4j
public class BizException extends RuntimeException {
    /**
     * 系统
     */
    private String system;
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误描述
     */
    private String desc;

    public BizException(String desc) {
        super(desc);
        this.system = SystemUtil.constants != null ? SystemUtil.constants.getName() : null;
        this.desc = desc;
        log.error("bizException", this);
    }

    public BizException(String code, String desc) {
        super(String.format("code:%s,msg:%s", code, desc));
        this.system = SystemUtil.constants != null ? SystemUtil.constants.getName() : null;
        this.code = code;
        this.desc = desc;
        log.error("bizException", this);
    }

    public BizException(Result result) {
        super(String.format("code:%s,msg:%s", result.getCode(), result.getDesc()));
        this.system = result.getSystem();
        this.code = result.getCode();
        this.desc = result.getDesc();
        log.error("bizException", this);
    }

    public BizException(ReturnCode returnCode) {
        super(String.format("code:%s,msg:%s", returnCode.getCode(), returnCode.getDesc()));
        this.system = returnCode.getSystem();
        this.code = returnCode.getCode();
        this.desc = returnCode.getDesc();
        log.error("bizException", this);
    }

    public BizException(ReturnCode returnCode, Throwable cause) {
        super(String.format("code:%s,msg:%s", returnCode.getCode(), returnCode.getDesc()), cause);
        this.system = returnCode.getSystem();
        this.code = returnCode.getCode();
        this.desc = returnCode.getDesc();
        log.error("bizException", this);
    }
}
